﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using Yonkly.Utility;

namespace Yonkly.Models
{
    public class MessageItem : BaseViewData
    {
        #region Properties

        public long ID { get; set; }

        public long? GroupId { get; set; }

        public string GroupUrl { get; set; }

        public long? ParentId { get; set; }

        public string ParentName { get; set; }

        public string Text { get; set; }

        public string ThumbnailUrl { get; set; }

        public string ImageUrl { get; set; }

        public MessageTypes Type { get; set; }

        public UserItem Sender { get; set; }

        public UserItem Recipient { get; set; }

        public DateTime CreatedOn { get; set; }

        public string PostedAgo
        {
            get { return CreatedOn.Ago(); }
        }

        public DateTime ModifiedOn { get; set; }

        public string ModifiedAgo
        {
            get { return ModifiedOn.Ago(); }
        }

        public string Html
        {
            get
            {
                return Utils.ConvertMessageToHtml(Text);
            }
        }

        public int ChildrenCount { get; set; }

        #endregion

        private void init(ViewMessage message)
        {
            ID = message.MessageId;
            ParentId = message.ParentId;
            ParentName = message.ParentName ?? string.Empty;
            ChildrenCount = message.ChildrenCount ?? 0;
            Text = message.Text;
            ThumbnailUrl = AmazonHelper.GetFileUrl(message.ThumbnailFilename);
            ImageUrl = AmazonHelper.GetFileUrl(message.ImageFilename);

            Sender = new UserItem
                         {
                             UserId = message.SenderId,
                             Email = message.SenderEmail,
                             UserName = message.SenderName
                         };
            Recipient = new UserItem
                            {
                                UserId = message.RecipientId != null ? (Guid)message.RecipientId : Guid.Empty,
                                Email = message.RecipientEmail,
                                UserName = message.RecipientName
                            };

            Type = (MessageTypes)message.Type;
            CreatedOn = message.CreatedOn;
            ModifiedOn = message.ModifiedOn;
        }


        private void init(ViewGroupMessage message)
        {
            ID = message.MessageId;
            ParentId = message.ParentId;
            ParentName = message.ParentName ?? string.Empty;
            ChildrenCount = message.ChildrenCount ?? 0;
            Text = message.Text;
            Sender = new UserItem
            {
                UserId = message.SenderId,
                Email = message.SenderEmail,
                UserName = message.SenderName
            };
            Recipient = new UserItem
            {
                UserId = message.RecipientId != null ? (Guid)message.RecipientId : Guid.Empty,
                Email = message.RecipientEmail,
                UserName = message.RecipientName
            };
            CreatedOn = message.CreatedOn;
            ModifiedOn = message.ModifiedOn;
            GroupId = message.GroupId;
            GroupUrl = message.GroupUrl;
        }



        public MessageItem(BaseViewData mybase, ViewMessage message)
            : base(mybase)
        {
            if (message != null)
            {
                init(message);
            }
        }

        public MessageItem(BaseViewData mybase, ViewGroupMessage message)
            : base(mybase)
        {
            if (message != null)
            {
                init(message);
            }
        }
    }

    public static class MessageItemExtensions
    {
        public static bool CanReply(this MessageItem msg)
        {
            if (!msg.IsAuthenticated)
                return false;

            using (var dc = new YonklyDataContext())
            {
                if (dc.GroupMembers.Count(gm => gm.GroupId == msg.GroupId &&
                    gm.UserId == msg.LoggedInUser.UserId &&
                    gm.Status == (int)GroupMemberStatuses.Approved) == 1)
                    return true;
            }
            return false;
        }
    }


    public enum MessageTypes
    {
        Text = 0,
        Image = 1
    }
}